package com.example.activity;


import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;

import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.Base.BaseActivity;
import com.example.administrator.olddoctor.R;
import com.example.tools.Voice.MediaManager;
import com.example.tools.Voice.Recorder;
import com.example.tools.Voice.AudioRecorderButton;

public class voiceDemoActivity extends BaseActivity {

    private AudioRecorderButton mAudioRecorderButton;
    private Recorder recorder;
    private int mMaxItemWith;
    private int mMinItemWith;

    private LinearLayout layout;
    private RelativeLayout recorder_length;
    private TextView recorder_time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texttext);
        mAudioRecorderButton = (AudioRecorderButton) findViewById(R.id.id_recorder_button);
        layout = (LinearLayout) findViewById(R.id.layout);
        recorder_time = (TextView) findViewById(R.id.recorder_time);
        recorder_length = (RelativeLayout) findViewById(R.id.recorder_length);
        mAudioRecorderButton = (AudioRecorderButton) findViewById(R.id.id_recorder_button);
        mAudioRecorderButton.setAudioFinishRecorderListener(new AudioRecorderButton.AudioFinishRecorderListener() {
            @Override
            public void onFinish(float seconds, String filePath) {
                recordCallBack(seconds,filePath);
            }
        });
    }

    public void recordCallBack(float seconds, String filePath){
        recorder = new Recorder(seconds, filePath);
        WindowManager wManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wManager.getDefaultDisplay().getMetrics(outMetrics);
        mMaxItemWith = (int) (outMetrics.widthPixels * 0.7f);
        mMinItemWith = (int) (outMetrics.widthPixels * 0.15f);// 获取系统宽度
        String str = Math.round(seconds)+"\"";
        recorder_time.setText(str);
        ViewGroup.LayoutParams params= recorder_length.getLayoutParams();
        params.width = (int) (mMinItemWith+mMaxItemWith/60f*recorder.getTime());
        recorder_length.setLayoutParams(params);
        recorder_length.setVisibility(View.VISIBLE);
    }

    public void playVoice(){





    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MediaManager.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MediaManager.resume();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        MediaManager.release();
    }

}
